import time
starttime = time.time()

npcList = ['派蒙', '凯亚', '安柏', '丽莎', '琴', '香菱', '枫原万叶', '迪卢克', '温迪', '可莉', '早柚', '托马', '芭芭拉', '优菈', '云堇', '钟离', '魈', '凝光', '雷电将军', '北斗', '甘雨', '七七', '刻晴', '神里绫华', '戴因斯雷布', '雷泽', '神里绫人', '罗莎莉亚', '阿贝多', '八重神子', '宵宫', '荒泷一斗', '九条裟罗', '夜兰', '珊瑚宫心海', '五郎', '散兵', '女士', '达达利亚', '莫娜', '班尼特', '申鹤', '行秋', '烟绯', '久岐忍', '辛焱', '砂糖', '胡桃', '重云', '菲谢尔', '诺艾尔', '迪奥娜', '鹿野院平藏']

import sys
out_path = sys.argv[1]
speaker = sys.argv[2]
t_mt = ' '.join(sys.argv[3:])

print('发言人：', speaker, '\n发言内容：', t_mt, sep='')
try:
  speakerid = npcList.index(speaker)
except ValueError:
  print('错误：发言人', speaker, '不存在', file = sys.stderr)
  exit(1)
print('正在加载模型……')

import os
import json
import math
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader

import commons
import utils
from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence

from scipy.io.wavfile import write

def get_text(text, hps):
    text_norm = text_to_sequence(text, hps.data.text_cleaners)
    if hps.data.add_blank:
        text_norm = commons.intersperse(text_norm, 0)
    text_norm = torch.LongTensor(text_norm)
    return text_norm

hps_mt = utils.get_hparams_from_file('configs/genshin.json')

net_g_mt = SynthesizerTrn(
    len(symbols),
    hps_mt.data.filter_length // 2 + 1,
    hps_mt.train.segment_size // hps_mt.data.hop_length,
    n_speakers=hps_mt.data.n_speakers,
    **hps_mt.model)
_ = net_g_mt.eval()

_ = utils.load_checkpoint('G_809000.pth', net_g_mt, None)

stn_tst_mt = get_text(t_mt, hps_mt)

with torch.no_grad():
    x_tst_mt = stn_tst_mt.unsqueeze(0)
    x_tst_mt_lengths = torch.LongTensor([stn_tst_mt.size(0)])
    sid_mt = torch.LongTensor([speakerid])
    audio_mt = net_g_mt.infer(x_tst_mt, x_tst_mt_lengths, sid=sid_mt, noise_scale=.667, noise_scale_w=.8, length_scale=1.2)[0][0,0].data.cpu().float().numpy()

print('正在输出到文件：', out_path, sep='')
write(out_path, hps_mt.data.sampling_rate, audio_mt)

print('生成用时：', time.time() - starttime, 's', sep='')